import { bind } from "../control/bind.js";
import {
  createScene,
  Toast,
  loadMarker,
  loadMeterial,
  findInRoom,
  rotationY,
} from "./components/loader.js";
export const name = "loadOutSide";

export function Scene({ changeScene, camera, renderer }) {
  var camera_time = 0;

  const { scene, mesh } = createScene();

  findInRoom(scene);
  loadMeterial(mesh, "outside");

  const onClick = (item) => {
    const name = item.name;
    if (name === "进门") {
      camera_time = 1;
      // outsideTextTip.visible = false;
    } else {
      Toast(`小偷不在${name}`, 2000, scene, getPosition);
    }
  };

  const interactPoints = [
    { name: "进门", scale: 4, x: 0, y: 1.5, z: 24, onClick },
    {
      name: "顶顶顶",
      scale: 1,
      x: 5,
      y: 3,
      z: 22,
      onClick,
    },
    { name: "汽车", scale: 3, x: 40, y: 1, z: -20, onClick },
    { name: "行人", scale: 3, x: -20, y: 1, z: -30, onClick },
    { name: "外面", scale: 3, x: 20, y: 1, z: -30, onClick },
  ];
  const { interactMeshes, anchorMeshes } = loadMarker(interactPoints, {
    scene,
  });
  const {
    getPosition,
    setPosition,
    unbind,
    lookAt,
    cameraUpdate,
    fovJian,
    reset,
  } = bind({
    camera,
    renderer,
    interactMeshes,
  });

  function update() {
    rotationY(anchorMeshes);
    if (camera_time === 50) {
      changeScene("loadInSide");
    } else if (camera_time > 0) {
      fovJian();
      camera_time++;
    } else {
      cameraUpdate();
    }
  }

  function destroy() {
    unbind();
  }

  reset();
  return { scene, update, destroy };
}
